//
//  ChoosePKViewController.m
//  ShrimpCrowdordering
//
//  Created by 郑洲 on 2018/7/31.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "ChoosePKViewController.h"
#import "CHCardView.h"
#import "CHCardItemCustomView.h"
#import "CHCardItemModel.h"

#import "PKListViewController.h"
#import "PKViewController.h"

#import "UIImage+GradientColor.h"

@interface ChoosePKViewController ()<CHCardViewDelegate, CHCardViewDataSource>

@property (nonatomic, strong) CHCardView *cardView;


@property (nonatomic, strong) CHCardItemModel *proModel;
@property (nonatomic, assign) NSInteger currentPage;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation ChoosePKViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setShadowImage:nil];
    UIView *dislikeView = [kKeyWindow viewWithTag:300];
    UIView *likeView = [kKeyWindow viewWithTag:301];
    [dislikeView removeFromSuperview];
    [likeView removeFromSuperview];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setUI];
    self.title = @"PK";
    self.currentPage = 1;
    self.dataArray = [NSMutableArray array];
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(49), ScreenX375(43), SCREEN_WIDTH - ScreenX375(98), ScreenX375(500))];
    view1.backgroundColor = [UIColor whiteColor];
    view1.layer.cornerRadius = ScreenX375(8);
    view1.layer.shadowOffset = CGSizeMake(0, 4); //设置阴影的偏移量
    view1.layer.shadowRadius = ScreenX375(8);  //设置阴影的半径
    view1.layer.shadowColor = RGBACOLOR(0, 0, 0, 0.11).CGColor; //设置阴影的颜色为黑色
    view1.layer.shadowOpacity = 1; //设置阴影的不透明度
    [self.view addSubview:view1];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(34), ScreenX375(28), SCREEN_WIDTH - ScreenX375(68), ScreenX375(500))];
    view2.backgroundColor = [UIColor whiteColor];
    view2.layer.cornerRadius = ScreenX375(8);
    view2.layer.shadowOffset = CGSizeMake(0, 4); //设置阴影的偏移量
    view2.layer.shadowRadius = ScreenX375(8);  //设置阴影的半径
    view2.layer.shadowColor = RGBACOLOR(0, 0, 0, 0.11).CGColor; //设置阴影的颜色为黑色
    view2.layer.shadowOpacity = 1; //设置阴影的不透明度
    [self.view addSubview:view2];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(19), ScreenX375(13), SCREEN_WIDTH - ScreenX375(38), ScreenX375(500))];
    view.backgroundColor = [UIColor whiteColor];
    view.layer.cornerRadius = ScreenX375(8);
    view.layer.shadowOffset = CGSizeMake(0, 4); //设置阴影的偏移量
    view.layer.shadowRadius = ScreenX375(8);  //设置阴影的半径
    view.layer.shadowColor = RGBACOLOR(0, 0, 0, 0.11).CGColor; //设置阴影的颜色为黑色
    view.layer.shadowOpacity = 1; //设置阴影的不透明度
    [self.view addSubview:view];
    
    _cardView = [[CHCardView alloc] initWithFrame:CGRectMake(ScreenX375(19), ScreenX375(13), SCREEN_WIDTH - ScreenX375(38), ScreenX375(500))];
    _cardView.delegate = self;
    _cardView.dataSource = self;
    [self.view addSubview:_cardView];
    
    UIButton *listBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenX375(50), ScreenX375(21))];
    [listBtn setTitle:@"PK记录" forState:UIControlStateNormal];
    [listBtn setTitleColor:REXADECIMALCOLOR(@"#222222") forState:UIControlStateNormal];
    listBtn.titleLabel.font = Kfont(14);
    [listBtn addTarget:self action:@selector(list) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:listBtn];
    
    [self getData];
}

- (void)getData {
    [HttpManager postHttpRequestByGet:[NSString stringWithFormat:@"%@api/getPkPro",REQUESTHEADER] andParameter:@{@"userId":kNSUDefaultReadKey(@"userId"),@"page":[NSNumber numberWithInteger:self.currentPage]} success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"200"]) {
            [self.dataArray removeAllObjects];
            for (NSDictionary *dic in successResponse[@"data"][@"list"]) {
                CHCardItemModel *model = [[CHCardItemModel alloc] initWithDic:dic];
                [self.dataArray addObject:model];
            }
            if (self.dataArray.count > 0) {
                self.proModel = self.dataArray[self.dataArray.count - 1];
            }
            [self.cardView reloadData];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

- (void)setUI {
    UIImage *bgImg = [UIImage gradientColorImageFromColors:@[REXADECIMALCOLOR(@"#ff1162"),REXADECIMALCOLOR(@"#fe4e4e")] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(SCREEN_WIDTH, ScreenX375(100) + STATUS_BAR_HEIGHT)];
    UIButton *pkBtn = [[UIButton alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - ScreenX375(120)) / 2, ScreenX375(550), ScreenX375(120), ScreenX375(38))];
    [pkBtn setTitle:@"PK" forState:UIControlStateNormal];
    [pkBtn setTitleColor:REXADECIMALCOLOR(@"#ffffff") forState:UIControlStateNormal];
    pkBtn.titleLabel.font = Kfont(24);
    [pkBtn setBackgroundImage:bgImg forState:UIControlStateNormal];
    pkBtn.layer.cornerRadius = ScreenX375(19);
    pkBtn.clipsToBounds = YES;
    [pkBtn addTarget:self action:@selector(pk) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:pkBtn];
    
    UIButton *disLikeBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(24), ScreenX375(508), ScreenX375(87), ScreenX375(82))];
    [disLikeBtn setImage:ImageWithName(@"破碎的灰色") forState:UIControlStateNormal];
    [disLikeBtn addTarget:self action:@selector(disLike:) forControlEvents:UIControlEventTouchUpInside];
    disLikeBtn.tag = 200;
//    [self.view addSubview:disLikeBtn];
    
    UIButton *likeBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - ScreenX375(111), ScreenX375(508), ScreenX375(87), ScreenX375(82))];
    [likeBtn setImage:ImageWithName(@"爱心") forState:UIControlStateNormal];
    [likeBtn addTarget:self action:@selector(like:) forControlEvents:UIControlEventTouchUpInside];
    likeBtn.tag = 201;
//    [self.view addSubview:likeBtn];
    
    UIView *dislikeView = [[UIView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - ScreenX375(113)) / 2, ScreenX375(244), ScreenX375(113), ScreenX375(113))];
    dislikeView.backgroundColor = [UIColor whiteColor];
    dislikeView.layer.cornerRadius = ScreenX375(8);
    dislikeView.clipsToBounds = YES;
    dislikeView.hidden = YES;
    dislikeView.tag = 300;
//    [kKeyWindow addSubview:dislikeView];
    
    UIImageView *dislikeImg = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(25), ScreenX375(31), ScreenX375(64), ScreenX375(51))];
    dislikeImg.image = ImageWithName(@"心2");
//    [dislikeView addSubview:dislikeImg];
    
    UIView *likeView = [[UIView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - ScreenX375(113)) / 2, ScreenX375(244), ScreenX375(113), ScreenX375(113))];
    likeView.backgroundColor = [UIColor whiteColor];
    likeView.layer.cornerRadius = ScreenX375(8);
    likeView.clipsToBounds = YES;
    likeView.hidden = YES;
    likeView.tag = 301;
//    [kKeyWindow addSubview:likeView];
    
    UIImageView *likeImg = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(25), ScreenX375(31), ScreenX375(64), ScreenX375(51))];
    likeImg.image = ImageWithName(@"心");
//    [likeView addSubview:likeImg];
}

- (void)concern {
    if ([_proModel.isFollow integerValue]) {
        _proModel.isFollow = @"0";
        [HttpManager showNoteMsg:@"取消关注成功"];
    }else {
        _proModel.isFollow = @"1";
        [HttpManager showNoteMsg:@"关注成功"];
    }
    [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@api/concern",REQUESTHEADER] andParameter:@{@"concernId":kNSUDefaultReadKey(@"userId"),@"concernedId":_proModel.userId} success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"200"]) {
            if ([self.proModel.isFollow integerValue]) {
                
            }else {

            }
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

- (void)disLike:(UIButton *)button {
    if (_proModel && _dataArray.count > 0) {
        UIView *view = [kKeyWindow viewWithTag:300];
        view.hidden = NO;
        [UIView animateWithDuration:0.5 animations:^{
            [button setImage:ImageWithName(@"破碎的拷贝") forState:UIControlStateNormal];
            button.transform = CGAffineTransformScale(button.transform, 1.1, 1.1);
            view.transform = CGAffineTransformScale(button.transform, 1.05, 1.05);
        } completion:^(BOOL finished) {
            [button setImage:ImageWithName(@"破碎的灰色") forState:UIControlStateNormal];
            button.transform = CGAffineTransformIdentity;
            view.transform = CGAffineTransformIdentity;
            view.hidden = YES;
        }];
        [self.cardView deleteTheTopItemViewWithLeft:YES];
        [self operateWithId:_proModel.proId andIsLike:NO];
    }
}

- (void)like:(UIButton *)button {
    if (_proModel && _dataArray.count > 0) {
        UIView *view = [kKeyWindow viewWithTag:301];
        view.hidden = NO;
        [UIView animateWithDuration:0.5 animations:^{
            [button setImage:ImageWithName(@"爱心拷贝") forState:UIControlStateNormal];
            button.transform = CGAffineTransformScale(button.transform, 1.1, 1.1);
            view.transform = CGAffineTransformScale(button.transform, 1.05, 1.05);
        } completion:^(BOOL finished) {
            [button setImage:ImageWithName(@"爱心") forState:UIControlStateNormal];
            button.transform = CGAffineTransformIdentity;
            view.transform = CGAffineTransformIdentity;
            view.hidden = YES;
        }];
        [self.cardView deleteTheTopItemViewWithLeft:NO];
        [self operateWithId:_proModel.proId andIsLike:YES];
    }
}

- (void)operateWithId:(NSString *)proId andIsLike:(BOOL)isLike {
//    [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@api/signIn",REQUESTHEADER] andParameter:@{@"userId":kNSUDefaultReadKey(@"userId"),@"proId":proId,@"isLike":[NSNumber numberWithBool:isLike]} success:^(id successResponse) {
//        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
//        if ([status isEqualToString:@"200"]) {
//
//        }else {
//            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
//        }
//    } andFailure:^(id failureResponse) {
//        [HttpManager showFail];
//    }];
}

- (void)list {
    PKListViewController *vc = [[PKListViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)pk {
    PKViewController *vc = [[PKViewController alloc] init];
    vc.model = _proModel;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - CHCardViewDelegate
- (NSInteger)numberOfItemViewsInCardView:(CHCardView *)cardView {
    return self.dataArray.count;
}

- (CHCardItemView *)cardView:(CHCardView *)cardView itemViewAtIndex:(NSInteger)index {
    CHCardItemCustomView *itemView = [[CHCardItemCustomView alloc] initWithFrame:cardView.bounds];
    itemView.itemModel = self.dataArray[index];
    return itemView;
}

- (void)cardViewCurrentPage:(NSInteger)index {
    if (_dataArray.count > index - 1) {
        CHCardItemModel *model = _dataArray[index - 1];
        _proModel = model;
    }else {
        [HttpManager showNoteMsg:@"暂无更多数据"];
    }
}

- (void)cardViewNeedMoreData:(CHCardView *)cardView {
    
    // data
    self.currentPage++;
    [self getData];
}

// select
- (void)cardView:(CHCardView *)cardView didClickItemAtIndex:(NSInteger)index {
    NSLog(@"%li",index);
}

- (void)cardView:(CHCardView *)cardView didRemoveItemAtIndex:(NSInteger)index withLeft:(BOOL)isleft {
    [self operateWithId:_proModel.proId andIsLike:!isleft];
//    if (isleft) {
//        UIButton *button = [self.view viewWithTag:200];
//        UIView *view = [kKeyWindow viewWithTag:300];
//        view.hidden = NO;
//        [UIView animateWithDuration:0.5 animations:^{
//            [button setImage:ImageWithName(@"破碎的拷贝") forState:UIControlStateNormal];
//            button.transform = CGAffineTransformScale(button.transform, 1.1, 1.1);
//            view.transform = CGAffineTransformScale(button.transform, 1.05, 1.05);
//        } completion:^(BOOL finished) {
//            [button setImage:ImageWithName(@"破碎的灰色") forState:UIControlStateNormal];
//            button.transform = CGAffineTransformIdentity;
//            view.transform = CGAffineTransformIdentity;
//            view.hidden = YES;
//        }];
//    }else {
//        UIButton *button = [self.view viewWithTag:201];
//        UIView *view = [kKeyWindow viewWithTag:301];
//        view.hidden = NO;
//        [UIView animateWithDuration:0.5 animations:^{
//            [button setImage:ImageWithName(@"爱心拷贝") forState:UIControlStateNormal];
//            button.transform = CGAffineTransformScale(button.transform, 1.1, 1.1);
//            view.transform = CGAffineTransformScale(button.transform, 1.05, 1.05);
//        } completion:^(BOOL finished) {
//            [button setImage:ImageWithName(@"爱心") forState:UIControlStateNormal];
//            button.transform = CGAffineTransformIdentity;
//            view.transform = CGAffineTransformIdentity;
//            view.hidden = YES;
//        }];
//    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
